package com.snifferx.test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.imageio.ImageIO;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean(name = "dynamicImageController")
@SessionScoped
public class DynamicImageController {
	private StreamedContent graphicText;
	ByteArrayOutputStream os ;

	public DynamicImageController() {
		try {
			// Graphic Text
			BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
			Graphics2D g2 = bufferedImg.createGraphics();
			g2.drawString("This is a text", 0, 10);
			os = new ByteArrayOutputStream();
			ImageIO.write(bufferedImg, "png", os);
			graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public StreamedContent getGraphicText() {
		if (graphicText != null) {
            graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
        }
        return graphicText;
	}

/*	public void setGraphicText(StreamedContent graphicText) {
		this.graphicText = graphicText;
	}*/
}
